package com.ah.ues.runner;

import com.ah.ues.utils.AesUtil;
import com.ah.ues.utils.GsonUtil;
import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkClient;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import shade.com.alibaba.fastjson2.JSONObject;

import java.util.Base64;

//@Component
public class DingTalkStreamRegister implements ApplicationRunner {
    private static final Logger log = LoggerFactory.getLogger(DingTalkStreamRegister.class);

    @Value("${dingTalk.clientId}")
    private String clientId;
    @Value("${dingTalk.clientSecret}")
    private String clientSecret;
    @Value("${aes.key}")
    private String aesKey;
    private static OpenDingTalkClient client;

    public static void stop() throws Exception {
        if (client != null) {
            client.stop();
        }
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // dingTalkRegister();
    }

    public String getClientSecret(){
        byte[] decode = Base64.getDecoder().decode(clientSecret.getBytes());
        byte[] decrypt = AesUtil.decrypt(decode, Base64.getDecoder().decode(aesKey.getBytes()));
        return new String(decrypt);
    }

    public static void dingTalkRegister(String clientId, String clientSecret) throws Exception {
        client = OpenDingTalkStreamClientBuilder
                .custom()
                .credential(new AuthClientCredential(clientId, clientSecret))
                //注册事件监听
                .registerAllEventListener(new GenericEventListener() {
                    public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                        try {
                            //事件唯一Id
                            String eventId = event.getEventId();
                            //事件类型
                            String eventType = event.getEventType();
                            //事件产生时间
                            Long bornTime = event.getEventBornTime();
                            //获取事件体
                            JSONObject bizData = event.getData();
                            //处理事件
                            process(bizData);
                            //消费成功
                            return EventAckStatus.SUCCESS;
                        } catch (Exception e) {
                            //消费失败
                            return EventAckStatus.LATER;
                        }
                    }
                })
                .build();
        try {
            client.start();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    private static void process(JSONObject bizData) {
        log.info("收到事件:{}", GsonUtil.obj2Json(bizData));
    }
}
